package com.SmartCare.service.impl.role;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.SmartCare.common.Result;
import com.SmartCare.common.vo.role.ElderRelaVo;
import com.SmartCare.pojo.role.UserRelationship;
import com.SmartCare.mapper.role.UserRelationshipMapper;
import com.SmartCare.service.role.ElderlyInformationService;
import com.SmartCare.service.role.UserRelationshipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户老人关系表 服务实现类
 */
@Service
public class UserRelationshipServiceImpl extends ServiceImpl<UserRelationshipMapper, UserRelationship> implements UserRelationshipService {
    @Autowired
    private ElderlyInformationService elderlyInformationService;
    /**
     * 获取家属与老人的绑定关系
     * @param userId
     * @return
     */
    @Override
    public List<ElderRelaVo> getElderIds(Long userId) {

        LambdaQueryWrapper<UserRelationship> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRelationship::getFamilyMemberId,userId);
        queryWrapper.eq(UserRelationship::getStatus,1);
        List<UserRelationship> list = list(queryWrapper);

        List<ElderRelaVo> elderVos=new ArrayList<>();

        list.forEach((item)->{
            ElderRelaVo vo = new ElderRelaVo();
            BeanUtils.copyProperties(item,vo,"name");
            String name = elderlyInformationService.getNameById(item.getElderlyId());
            vo.setName(name);
            elderVos.add(vo);
        });
        return elderVos;
    }

    /**
     * 添加绑定老人信息
     * @param elderlyId
     * @param familyId
     * @param relationship
     * @return
     */
    @Override
    public Result addFamilyInfo(Long elderlyId, Long familyId, String relationship) {
        if (elderlyId==null || familyId==null || relationship==null){
            return Result.error("参数请求错误");
        }
        UserRelationship userRelationship = UserRelationship.builder()
                .elderlyId(elderlyId)
                .familyMemberId(familyId)
                .relationship(relationship)
                .build();
        boolean res = save(userRelationship);
        if(!res){
            return Result.error("绑定失败");
        }
        return Result.success("添加成功");
    }
}
